<?php
/**
 * egpusersDownload.php
 * 
 * (C) 2009, tetsuya.odaka(EzoGP).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* 
 * EGP_USERSをCSV形式でダウンロードする。
 * 
 * author；		tetsuya.odaka
 * date;		2008/6/2
 *  
*/

	// 前準備
	include("../Myznala/myHeader.inc.php");
	
	// コネクションの取得
	require_once('../Myznala/MyDBConnection.class.php');
	$cObj = new MyDBConnection();
	$conn=$cObj->getConnection();

	require_once('EgpusersDao.class.php');
	$dObj=new EgpusersDao();

	$data_array=$dObj->getAll($conn);
	
	// connectionのリリース
	$cObj->closeConnection($conn); 
		
	//見出しの編集  
	$data="id";
	$data.=",";
	$data.=mb_convert_encoding("Email","Shift_JIS","UTF-8");
	$data.=",";
	$data.=mb_convert_encoding("パスワード","Shift_JIS","UTF-8");
	$data.=",";
	$data.=mb_convert_encoding("名前","Shift_JIS","UTF-8");
	$data.=",";
	$data.=mb_convert_encoding("よみ","Shift_JIS","UTF-8");
	$data.=",";
	$data.=mb_convert_encoding("役割","Shift_JIS","UTF-8");
	$data.="\n";

//	$myLog->debug("count(data_array)".count($data_array));
//	$myLog->debug("data_array[0]".$data_array);
	
	for($i=0;$i<count($data_array);$i++){
		$wrk = $data_array[$i];
		$data.=$wrk->getId();
		$data.= ",";
		$data.=mb_convert_encoding($wrk->getEmail(),'Shift-JIS','UTF-8');
		$data.= ",";
		$data.=mb_convert_encoding($wrk->getPassword(),'Shift-JIS','UTF-8');
		$data.= ",";
		$data.=mb_convert_encoding($wrk->getName(),'Shift-JIS','UTF-8');
		$data.= ",";
		$data.=mb_convert_encoding($wrk->getNameYomi(),'Shift-JIS','UTF-8');
		$data.= ",";
		$data.=mb_convert_encoding($wrk->getRole(),'Shift-JIS','UTF-8');
		$data.= "\n";
	}

	// ダウンロードの処理開始
	require_once("../PEAR/HTTP/Download.php");
	$fname = 'egpusers'.date("Ymd_His").'.csv';
	$params=array(
		"data"=>$data,
		"contenttype"=>"application/octet-stream",
		"contentdisposition"=>array(HTTP_DOWNLOAD_ATTACHMENT,$fname)
	);
	HTTP_Download::staticSend($params);

	// ログの出力(end)
//	$myLog->debug("$myPageName: end");
	// ストリームの吐き出し
	$dl->send();
?>